package com.yanfan.zutai.controller;

import com.yanfan.common.annotation.Log;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.page.TableDataInfo;
import com.yanfan.common.enums.BusinessType;
import com.yanfan.common.utils.SecurityUtils;
import com.yanfan.common.utils.poi.ExcelUtil;
import com.yanfan.zutai.aop.SysInitData;
import com.yanfan.zutai.aop.SysInitUpdateData;
import com.yanfan.zutai.domain.BShortMessage;
import com.yanfan.zutai.service.IBShortMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 短信分配Controller
 *
 * @author ruoyi
 * @date 2022-06-10
 */
@RestController
@RequestMapping("/zutai/bShortMessage")
public class BShortMessageController extends BaseController {
    @Autowired
    private IBShortMessageService bShortMessageService;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询短信分配列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BShortMessage bShortMessage) {
        startPage();
        if (!SecurityUtils.getUsername().equals("admin")) {
            bShortMessage.setUserName(SecurityUtils.getUsername());
        }
        List<BShortMessage> list = bShortMessageService.selectBShortMessageList(bShortMessage);
        return getDataTable(list);
    }

    /**
     * 导出短信分配列表
     */
    @Log(title = "短信分配", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BShortMessage bShortMessage) {
        List<BShortMessage> list = bShortMessageService.selectBShortMessageList(bShortMessage);
        ExcelUtil<BShortMessage> util = new ExcelUtil<BShortMessage>(BShortMessage.class);
        util.exportExcel(response, list, "短信分配数据");
    }

    /**
     * 获取短信分配详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bShortMessageService.selectBShortMessageById(id));
    }

    /**
     * 新增短信分配
     */
    @Log(title = "短信分配", businessType = BusinessType.INSERT)
    @PostMapping
    @SysInitData
    public AjaxResult add(@RequestBody BShortMessage bShortMessage) {
        bShortMessage.setShortCount(bShortMessage.getAllocationCount());
        bShortMessage.setShortUseCount(0);
        bShortMessage.setShortNotUseCount(bShortMessage.getShortCount() - bShortMessage.getShortUseCount());
        Integer count = jdbcTemplate.queryForObject("select count(1) from b_short_message where user_name=? ", Integer.class, bShortMessage.getUserName());
        if (count > 0) {
            return AjaxResult.error("此分配用户已存在，新增失败");
        }
        return toAjax(bShortMessageService.insertBShortMessage(bShortMessage));
    }

    /**
     * 修改短信分配
     */
    @Log(title = "短信分配", businessType = BusinessType.UPDATE)
    @PutMapping
    @SysInitUpdateData
    public AjaxResult edit(@RequestBody BShortMessage bShortMessage) {
        bShortMessage.setShortCount(bShortMessage.getShortCount() + bShortMessage.getAllocationCount());
        bShortMessage.setShortNotUseCount(bShortMessage.getShortCount() - bShortMessage.getShortUseCount());
        Integer count = jdbcTemplate.queryForObject("select count(1) from b_short_message where user_name=? and id !=? ", Integer.class, bShortMessage.getUserName(), bShortMessage.getId());
        if (count > 0) {
            return AjaxResult.error("此分配用户已存在，分配失败");
        }
        return toAjax(bShortMessageService.updateBShortMessage(bShortMessage));
    }

    /**
     * 删除短信分配
     */
    @Log(title = "短信分配", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bShortMessageService.deleteBShortMessageByIds(ids));
    }
}
